<?xml version="1.0" encoding="UTF-8" standalone='no'?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

    <changeSet author="vsurubkov" id="19-05-2023-articles-add-table-articles">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="articles"/>
            </not>
        </preConditions>

        <createTable tableName="articles">
            <column name="id_article" type="bigint" autoIncrement="true">
                <constraints primaryKey="true" primaryKeyName="pk_article"/>
            </column>
            <column name="title" type="nvarchar(256)">
                <constraints nullable="false" unique="true"/>
            </column>
            <column name="id_creator" type="bigint">
                <constraints nullable="false"/>
            </column>
            <column name="creation_date" type="timestamp with time zone">
                <constraints nullable="false"/>
            </column>
            <column name="text" type="text">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet author="vsurubkov" id="19-05-2023-articles-add-table-creators">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="creators"/>
            </not>
        </preConditions>

        <createTable tableName="creators">
            <column name="id_creator" type="bigint" autoIncrement="true">
                <constraints primaryKey="true" primaryKeyName="pk_creator"/>
            </column>
            <column name="creator" type="nvarchar(64)">
                <constraints nullable="false" unique="true"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet author="vsurubkov" id="19-05-2023-articles-add-fk-creator-for-table-articles">
        <preConditions onFail="MARK_RAN">
            <not>
                <foreignKeyConstraintExists foreignKeyName="fk_creator_in_articles" foreignKeyTableName="articles"/>
            </not>
        </preConditions>

        <addForeignKeyConstraint constraintName="fk_creator_in_articles"
                                 baseTableName="articles"
                                 baseColumnNames="id_creator"
                                 referencedTableName="creators"
                                 referencedColumnNames="id_creator"
        />
    </changeSet>

    <changeSet author="vsurubkov" id="19-05-2023-articles-add-table-categories">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="categories"/>
            </not>
        </preConditions>

        <createTable tableName="categories">
            <column name="id_category" type="bigint" autoIncrement="true">
                <constraints primaryKey="true" primaryKeyName="pk_category"/>
            </column>
            <column name="category" type="nvarchar(64)">
                <constraints nullable="false" unique="true"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet author="vsurubkov" id="19-05-2023-articles-add-table-articles-categories">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="articles_categories"/>
            </not>
        </preConditions>

        <createTable tableName="articles_categories">
            <column name="id_article" type="bigint">
                <constraints nullable="false"/>
            </column>
            <column name="id_category" type="bigint">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

    <changeSet author="vsurubkov" id="19-04-2023-artiles-add-fk-article-for-table-articles-categories">
        <preConditions onFail="MARK_RAN">
            <not>
                <foreignKeyConstraintExists foreignKeyName="fk_article_in_articles_categories"
                                            foreignKeyTableName="articles_categories"/>
            </not>
        </preConditions>

        <addForeignKeyConstraint constraintName="fk_article_in_articles_categories"
                                 baseTableName="articles_categories"
                                 baseColumnNames="id_article"
                                 referencedTableName="articles"
                                 referencedColumnNames="id_article"
        />
    </changeSet>

    <changeSet author="vsurubkov" id="19-04-2023-artiles-add-fk-category-for-table-articles-categories">
        <preConditions onFail="MARK_RAN">
            <not>
                <foreignKeyConstraintExists foreignKeyName="fk_category_in_articles_categories"
                                            foreignKeyTableName="articles_categories"/>
            </not>
        </preConditions>

        <addForeignKeyConstraint constraintName="fk_category_in_articles_categories"
                                 baseTableName="articles_categories"
                                 baseColumnNames="id_category"
                                 referencedTableName="categories"
                                 referencedColumnNames="id_category"
        />
    </changeSet>

</databaseChangeLog>